﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LambdaProj
{
    public class Department
    {
        public long Id { set; get; }
        public string Name { set; get; }
        public override string ToString()
        {
            return string.Format("{0}",Name);
        }

    }

    public class Employee
    {
        public long Id { set; get; }
        public string Name { set; get; }

        public string Title { set; get; }

        public int DepartmentID { set; get; }
        public override string ToString()
        {
            return string.Format("{0},{1}", Name,Title);
        }

    }

    public static class CorporateData
    {
        public static readonly Department[] Departments = new Department[] { 
          new Department(){
           Name = "Bin", Id = 1
          },
          new Department(){
           Name = "BinZhuo", Id = 2
          }
        };

        public static readonly Employee[] Employees = new Employee[] {
          new Employee(){
           Title = "GLD", DepartmentID = 1, Id = 1, Name = "Zhuo"
          }
        };

        public static void Print<T>(IEnumerable<T> items)
        {
            foreach(T item in items)
            {
                Console.WriteLine(item);
            }
        }

        public static void RunPrint()
        {
            Print(CorporateData.Departments);
            Print(CorporateData.Employees);
        }

        public static void JoinAction()
        {
            var items = Employees.Join(Departments, employee => employee.DepartmentID, deppartment => deppartment.Id, (emp, depart) => new
            {
                emp.Id,
                emp.Name,
                emp.Title,
                Department = depart
            });
            foreach(var item in items)
            {
                Console.WriteLine("{0} ({1})",item.Name,item.Title);
                Console.WriteLine("\t"+item.Department);
            }
        }

        public static void GroupByDemo()
        {
            IEnumerable<IGrouping<int, Employee>> groupedEmployees =
                Employees.GroupBy(emp => emp.DepartmentID);

            foreach(IGrouping<int,Employee> empGroup in groupedEmployees)
            {
                Console.WriteLine();
                foreach(Employee emp in empGroup)
                {
                    Console.WriteLine("\t"+emp);
                }
                Console.WriteLine("\tCount:"+empGroup.Count());
            }
        }
    }
}
